home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / components / sbIPlaylistReaderAtom.js < prev    next >
Text File  |  2006-02-08  |  11KB  |  383 lines

  1. /*
  2.  //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. // sbIPlaylistReader Object (RSS)
  29. //
  30.  
  31. const SONGBIRD_PLAYLISTATOM_CONTRACTID = "@songbird.org/Songbird/Playlist/Reader/Atom;1";
  32. const SONGBIRD_PLAYLISTATOM_CLASSNAME = "Songbird Atom Playlist Interface";
  33. const SONGBIRD_PLAYLISTATOM_CID = Components.ID("{9618AFE8-37CA-4fc9-A700-88205294F637}");
  34. const SONGBIRD_PLAYLISTATOM_IID = Components.interfaces.sbIPlaylistReader;
  35.  
  36. function CPlaylistAtom()
  37. {
  38.   jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
  39.   jsLoader.loadSubScript( "chrome://rmp_demo/content/songbird_interfaces.js", this );
  40. }
  41.  
  42. /* I actually need a constructor in this case. */
  43. CPlaylistAtom.prototype.constructor = CPlaylistAtom;
  44.  
  45. /* the CPlaylistAtom class def */
  46. CPlaylistAtom.prototype = 
  47. {
  48.   originalURL: "",
  49.  
  50.   m_document: null,
  51.   m_playlistmgr: null,
  52.   m_playlist: null,
  53.   m_library: null,
  54.   m_query: null,
  55.   
  56.   m_guid: "",
  57.   m_table: "",
  58.   m_append: false,
  59.   
  60.   Read: function( strURL, strGUID, strDestTable, bAppendOrReplace, /* out */ errorCode )
  61.   {
  62.     try
  63.     {
  64.       errorCode.value = 0;
  65.  
  66.       var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"].createInstance(Components.interfaces.nsIDOMParser);
  67.       var pFileReader = (Components.classes["@mozilla.org/network/file-input-stream;1"]).createInstance(Components.interfaces.nsIFileInputStream);
  68.       var pFile = (Components.classes["@mozilla.org/file/local;1"]).createInstance(Components.interfaces.nsILocalFile);
  69.       var pURI = (Components.classes["@mozilla.org/network/simple-uri;1"]).createInstance(Components.interfaces.nsIURI);
  70.  
  71.       if ( domParser && pFile && pURI && pFileReader )
  72.       {
  73.         pURI.spec = strURL;
  74.         if ( pURI.scheme == "file" )
  75.         {
  76.           var cstrPathToPLS = pURI.path;
  77.           cstrPathToPLS = cstrPathToPLS.substr( 3, cstrPathToPLS.length );
  78.           pFile.initWithPath(cstrPathToPLS);
  79.           if ( pFile.isFile() )
  80.           {
  81.             pFileReader.init( pFile, /* PR_RDONLY */ 1, 0, /*nsIFileInputStream::CLOSE_ON_EOF*/ 0 );
  82.             var document = domParser.parseFromStream(pFileReader, null, pFileReader.available(), "application/xml");
  83.             pFileReader.close();
  84.             
  85.             if(document)
  86.             {
  87.               const MediaLibrary = new Components.Constructor("@songbird.org/Songbird/MediaLibrary;1", "sbIMediaLibrary");
  88.               const PlaylistManager = new Components.Constructor("@songbird.org/Songbird/PlaylistManager;1", "sbIPlaylistManager");
  89.  
  90.               this.m_guid = strGUID;
  91.               this.m_table = strDestTable;
  92.               this.m_append = bAppendOrReplace;
  93.               
  94.               this.m_query = new this.sbIDatabaseQuery();
  95.               this.m_query.SetAsyncQuery(true);
  96.               this.m_query.SetDatabaseGUID(strGUID);
  97.  
  98.               this.m_document = document;
  99.               this.m_library = new MediaLibrary();
  100.               this.m_playlistmgr = new PlaylistManager();
  101.               
  102.               this.m_library.SetQueryObject(this.m_query);
  103.               var playlist = this.m_playlistmgr.GetPlaylist(strDestTable, this.m_query);
  104.               
  105.               if(playlist)
  106.               {
  107.                 this.m_playlist = playlist;
  108.                 return this.ProcessXMLDocument(this.m_document);
  109.               }
  110.             }
  111.           }
  112.         }
  113.       }
  114.     }
  115.     catch ( err )
  116.     {
  117.       throw "CPlaylistAtom::Read - " + err;
  118.     }
  119.     
  120.     return false;
  121.   },
  122.   
  123.   Vote: function( strURL )
  124.   {
  125.     try
  126.     {
  127.       return 10000;
  128.     }
  129.     catch ( err )
  130.     {
  131.       throw "CPlaylistAtom::Vote - " + err;
  132.     }
  133.   },
  134.   
  135.   Name: function()
  136.   {
  137.     try
  138.     {
  139.       return "Atom Playlist"; // ????
  140.     }
  141.     catch ( err )
  142.     {
  143.       throw "CPlaylistAtom::Name - " + err;
  144.     }
  145.   },
  146.   
  147.   Description: function()
  148.   {
  149.     try
  150.     {
  151.       return "Atom Playlist"; // ????
  152.     }
  153.     catch ( err )
  154.     {
  155.       throw "CPlaylistAtom::Description - " + err;
  156.     }
  157.   },
  158.   
  159.   SupportedMIMETypes: function( /* out */ nMIMECount )
  160.   {
  161.     var retval = new Array;
  162.     nMIMECount.value = 0;
  163.     
  164.     try
  165.     {
  166.       retval.push( "text/xml" );
  167.       retval.push( "text/atom+xml" );
  168.       retval.push( "application/xml" );
  169.       
  170.       nMIMECount.value = retval.length;
  171.     }
  172.     catch ( err )
  173.     {
  174.       throw "CPlaylistAtom::SupportedMIMETypes - " + err;
  175.     }
  176.     return retval;
  177.   },
  178.   
  179.   SupportedFileExtensions: function( /* out */ nExtCount )
  180.   {
  181.     var retval = new Array;
  182.     nExtCount.value = 0;
  183.     
  184.     try
  185.     {
  186.       retval.push( "xml" );
  187.       retval.push( "atom" );
  188.  
  189.       nExtCount.value = retval.length;
  190.     }
  191.     catch ( err )
  192.     {
  193.       throw "CPlaylistAtom::SupportedFileExtensions - " + err;
  194.     }
  195.     
  196.     return retval;
  197.   },
  198.   
  199.   ProcessXMLDocument: function( xmlDocument )
  200.   {
  201.     var ret = false;
  202.     try
  203.     {
  204.       var document = xmlDocument.documentElement;
  205.       dump("CPlaylistAtom::ProcessXMLDocument - Tag Name: " + document.tagName + "\n");
  206.       
  207.       if(document.tagName == "feed")
  208.       {
  209.         var atomVersion = document.getAttribute("version");
  210.         dump("CPlaylistAtom::ProcessXMLDocument - Atom Version: " + atomVersion + "\n");
  211.         
  212.         if(atomVersion[0] == "1" ||
  213.            atomVersion == "0.3" )
  214.         {
  215.           ret = this.ProcessAtomFeed(document);
  216.         }
  217.       }
  218.     }      
  219.     catch(err)
  220.     {
  221.       throw "CPlaylistAtom::ProcessXMLDocument - " + err;
  222.     }
  223.     
  224.     return ret;
  225.   },
  226.   
  227.   ProcessAtomFeed: function( xmlDocument )
  228.   {
  229.     try
  230.     {
  231.       
  232.     }
  233.     catch(err)
  234.     {
  235.       throw "CPlaylistAtom::ProcessAtomDocument - " + err;
  236.     }
  237.     
  238.     return false;
  239.   },
  240.   
  241.   IsMediaUrl: function( the_url )
  242.   {
  243.     if ( ( the_url.indexOf ) && 
  244.           (
  245.             // Protocols at the beginning
  246.             ( the_url.indexOf( "mms:" ) == 0 ) || 
  247.             ( the_url.indexOf( "rtsp:" ) == 0 ) || 
  248.             // File extensions at the end
  249.             ( the_url.indexOf( ".pls" ) != -1 ) || 
  250.             ( the_url.indexOf( ".m3u" ) == ( the_url.length - 4 ) ) || 
  251. //            ( the_url.indexOf( ".rm" ) == ( the_url.length - 3 ) ) || 
  252. //            ( the_url.indexOf( ".ram" ) == ( the_url.length - 4 ) ) || 
  253. //            ( the_url.indexOf( ".smil" ) == ( the_url.length - 5 ) ) || 
  254.             ( the_url.indexOf( ".mp3" ) == ( the_url.length - 4 ) ) ||
  255.             ( the_url.indexOf( ".m4a" ) == ( the_url.length - 4 ) ) ||
  256.             ( the_url.indexOf( ".ogg" ) == ( the_url.length - 4 ) ) ||
  257.             ( the_url.indexOf( ".wma" ) == ( the_url.length - 4 ) ) ||
  258.             ( the_url.indexOf( ".wmv" ) == ( the_url.length - 4 ) ) ||
  259.             ( the_url.indexOf( ".asx" ) == ( the_url.length - 4 ) ) ||
  260.             ( the_url.indexOf( ".asf" ) == ( the_url.length - 4 ) ) ||
  261.             ( the_url.indexOf( ".avi" ) == ( the_url.length - 4 ) ) ||
  262.             ( the_url.indexOf( ".mov" ) == ( the_url.length - 4 ) ) ||
  263.             ( the_url.indexOf( ".mp4" ) == ( the_url.length - 4 ) )
  264.           )
  265.         )
  266.     {
  267.       return true;
  268.     }
  269.     return false;
  270.   },
  271.   
  272.   ConvertUrlToDisplayName: function( url )
  273.   {
  274.     // Set the title display  
  275.     var the_value = "";
  276.     if ( url.lastIndexOf('/') != -1 )
  277.     {
  278.       the_value = url.substring( url.lastIndexOf('/') + 1, url.length );
  279.     }
  280.     else if ( url.lastIndexOf('\\') != -1 )
  281.     {
  282.       the_value = url.substring( url.lastIndexOf('\\') + 1, url.length );
  283.     }
  284.     else
  285.     {
  286.       the_value = url;
  287.     }
  288.     // Convert any %XX to space
  289.     var percent = the_value.indexOf('%');
  290.     if ( percent != -1 )
  291.     {
  292.       var remainder = the_value;
  293.       the_value = "";
  294.       while ( percent != -1 )
  295.       {
  296.         the_value += remainder.substring( 0, percent );
  297.         remainder = remainder.substring( percent + 3, url.length );
  298.         percent = remainder.indexOf('%');
  299.         the_value += " ";
  300.         if ( percent == -1 )
  301.         {
  302.           the_value += remainder;
  303.         }
  304.       }
  305.     }
  306.     if ( the_value.length == 0 )
  307.     {
  308.       the_value = url;
  309.     }
  310.     return the_value;
  311.   },
  312.  
  313.   QueryInterface: function(iid)
  314.   {
  315.       if (!iid.equals(Components.interfaces.nsISupports) &&
  316.           !iid.equals(SONGBIRD_PLAYLISTATOM_IID))
  317.           throw Components.results.NS_ERROR_NO_INTERFACE;
  318.       return this;
  319.   }
  320. }; //CPlaylistAtom
  321.  
  322. /**
  323.  * \class sbPlaylistAtomModule
  324.  * \brief 
  325.  */
  326. var sbPlaylistAtomModule = 
  327. {
  328.   registerSelf: function(compMgr, fileSpec, location, type)
  329.   {
  330.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  331.     compMgr.registerFactoryLocation(SONGBIRD_PLAYLISTATOM_CID, 
  332.                                     SONGBIRD_PLAYLISTATOM_CLASSNAME, 
  333.                                     SONGBIRD_PLAYLISTATOM_CONTRACTID, 
  334.                                     fileSpec, 
  335.                                     location,
  336.                                     type);
  337.   },
  338.  
  339.   getClassObject: function(compMgr, cid, iid) 
  340.   {
  341.     if(!cid.equals(SONGBIRD_PLAYLISTATOM_CID))
  342.         throw Components.results.NS_ERROR_NO_INTERFACE;
  343.  
  344.     if(!iid.equals(Components.interfaces.nsIFactory))
  345.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  346.  
  347.     return sbPlaylistAtomFactory;
  348.   },
  349.  
  350.   canUnload: function(compMgr)
  351.   { 
  352.     return true; 
  353.   }
  354. }; //sbPlaylistAtomModule
  355.  
  356. /**
  357.  * \class sbPlaylistAtomFactory
  358.  * \brief 
  359.  */
  360. var sbPlaylistAtomFactory =
  361. {
  362.   createInstance: function(outer, iid)
  363.   {
  364.     if (outer != null)
  365.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  366.  
  367.     if (!iid.equals(SONGBIRD_PLAYLISTATOM_IID) &&
  368.         !iid.equals(Components.interfaces.nsISupports))
  369.         throw Components.results.NS_ERROR_INVALID_ARG;
  370.  
  371.     return (new CPlaylistAtom()).QueryInterface(iid);
  372.   }
  373. }; //sbPlaylistAtomFactory
  374.  
  375. /**
  376.  * \function NSGetModule
  377.  * \brief 
  378.  */
  379. function NSGetModule(comMgr, fileSpec)
  380.   return sbPlaylistAtomModule;
  381. } //NSGetModule
  382.